//
//  UWTableViewCell.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-11-29.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "UWTableViewCell.h"
#import "UWGamesManager.h"
#import "UWFriendManager.h"
#import <QuartzCore/CALayer.h>

@implementation UWTableViewCell

@synthesize parent;

@synthesize leftCell, centerCell, rightCell, cells;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        // Initialization code
        [self setMultipleTouchEnabled:NO];

        leftCell = [[UWCellCol alloc] init];
        centerCell = [[UWCellCol alloc] init];
        rightCell = [[UWCellCol alloc] init];
        
        [self addSubview:leftCell.view];
        [self addSubview:centerCell.view];
        [self addSubview:rightCell.view];
        
        cells = [[NSArray alloc] initWithObjects:leftCell, centerCell, rightCell, nil];
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    //[super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setAtIndexPath:(NSIndexPath *)indexPath
{
    int i = 0;
    for (UWCellCol *c in cells) {
        // Configure the cell
        UWGame *game = [[UWGamesManager sharedManager] getGameAt:indexPath.row*3 + i];
        
        if (game != nil) {
            
            double increase = 1.0;
            int retinaY = 0;
            
            if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) {
                increase = 0.0;
                retinaY = 1;
            }
            
            [c setGame:game];
            
            CGRect frame;
            
            switch (i) {
                case 0:
                    c.borderShade.image = [UIImage imageNamed:@"left-cell"];
                    frame = c.borderShade.frame;
                    frame.size.width = 89;
                    frame.size.height = 91;
                    frame.origin.y = 23;
                    frame.origin.x = (106-89)/2 + 1;
                    c.borderShade.frame = frame;
                    break;
                case 1:
                    c.borderShade.image = [UIImage imageNamed:@"center-cell"];
                    frame = c.borderShade.frame;
                    frame.size.width = 88;
                    frame.size.height = 87;
                    frame.origin.y = 24;
                    frame.origin.x = (106-88)/2;
                    c.borderShade.frame = frame;
                    break;
                case 2:
                    c.borderShade.image = [UIImage imageNamed:@"right-cell"];
                    frame = c.borderShade.frame;
                    frame.size.width = 89;
                    frame.size.height = 90;
                    frame.origin.y = 23 + retinaY;
                    frame.origin.x = (106-89)/2 + 1;
                    c.borderShade.frame = frame;
                    break;
            }
        
            UWFriend *opponent = [[UWFriendManager sharedManager] getFriend:game.opponent];
            
            c.name.text = [opponent.first_name length] != 0 ? opponent.first_name : ([opponent.name length] != 0? opponent.name : @"");
            
            // Next the path for the profile image
            NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@-large.png", docDir, game.opponent];
            
            // check if image exists
            if ([[NSFileManager defaultManager] fileExistsAtPath:pngFilePath]) {
                c.image.image = [UIImage imageWithContentsOfFile:pngFilePath];
            } else {
                // Try to save the image
                [[UWFriendManager sharedManager] performSelectorInBackground:@selector(downloadLargeImage:) withObject:opponent];
            }
            
            c.subtext.text = [NSString stringWithFormat:@"%d - %d", game.p1score, game.p2score];
            
            //if (increase == 0) { // retina
                c.image.contentMode = UIViewContentModeScaleAspectFill;
            //}
            
            c.image.layer.cornerRadius = 6.0 + increase;
            c.image.layer.masksToBounds = YES;
        
            [game release];
            [opponent release];
        
            i++;
            
        } else {
            [c.borderShade setHidden:YES];
        }
    }
    
         
    /*
     
    // set pictures to table when loaded
     // TODO: change this to work with new grid view
    
     if (!opponent.table) {
     opponent.table = tableView;
     opponent.indexPath = indexPath;
     }
     
     */
    
}

- (void)layoutSubviews
{
    CGRect contentRect = self.contentView.bounds;
    CGFloat boundsX = contentRect.origin.x;
    CGRect frame;
    
    frame = CGRectMake(boundsX+6, 0, 106, 151);
    leftCell.view.frame = frame;
    
    frame = CGRectMake(boundsX+107, 0, 106, 151);
    centerCell.view.frame = frame;
    
    frame = CGRectMake(boundsX+208, 0, 106, 151);
    rightCell.view.frame = frame;
}

@end
